// HTTP 请求工具模块
const BASE_URL = 'http://192.168.1.10:8080'; // 根据实际API地址修改

export const http = {
    async get(endpoint, params) {
        try {
            const query = new URLSearchParams(params).toString();
            const response = await fetch(`${BASE_URL}/${endpoint}?${query}`, {});
            if (!response.ok) throw new Error(`HTTP error! status: ${response.status}`);
            return await response.json();
        } catch (error) {
            console.error('GET请求失败:', error);
            throw error;
        }
    },

    async post(endpoint, data) {
        try {
            const response = await fetch(`${BASE_URL}/${endpoint}`, {
                method: 'POST',
                body: JSON.stringify(data)
            });
            if (!response.ok) throw new Error(`HTTP error! status: ${response.status}`);
            return await response.json();
        } catch (error) {
            console.error('POST请求失败:', error);
            throw error;
        }
    },
    async getBook(id) {
        return  await http.get('book/get', { id });
    },
    // 创建书籍
    async createBook(bookData) {
        return await http.post('book/create', bookData);
    },
    // 更新书籍
    async updateBook(data) {
       return await http.post('book/update', data);
    },

    // 删除书籍
    async deleteBook(id) {
        await http.get("book/delete", { id });
    },
    //获取全部书籍
    async getBookList() {
        return await http.get('book/list');
    },

    // 创建章节
    async createChapter(data) {
       return await http.post('chapter/create',data);
    },

    // 更新章节
    async updateChapter(data) {
        await http.post('chapter/update', {
            ...data
        });
    },

    // 删除章节
    async deleteChapter(id) {
       return await http.get(`chapter/delete`, { id });
    },

    // 获取章节详情
    async getChapter(id) {
        return await http.get("chapter/get",{id});
       
    },
    // 获取章节列表
    async getChapterList(data) {
        return await http.get("chapter/list", data);       
    },
    async getEleList(data) {
        return await http.get("ele/list", data);
    },
    async createEle(data) {
        return await http.post("ele/create", data);
    },
    async updateEle(data) {
        return await http.post("ele/update", data);
    },
    async deleteEle(id) {
        return await http.get("ele/delete", { id });
    },
    async getEle(id) {
        return await http.get("ele/get", { id });
    },
    async searchEle(data) {
        return await http.post("ele/search", data); 
    },
    async getChapterlast(data){
        return await http.get("chapter/last",data);
    }

};

